#include "ChessProcessor.h"
#include "preprocess.h"
#include "chessdatabase.h"
#include "FeatureExtractor.h"

void printChessPiece(const ChessDatabase::ChessPiece& piece) {
    using namespace std;
    
    // 设置输出格式
    ios_base::fmtflags original_flags = cout.flags();
    cout << fixed << setprecision(4);
    const int col_width = 18;  // 统一列宽
    // 基础信息
    cout << "┌──────────────────────── ChessPiece #" << piece.num << " ────────────────────────┐\n";
    cout << left 
         << setw(col_width) << "| Position:"   << "| (" << piece.x << ", " << piece.y << ")\n"
         << setw(col_width) << "| Color:"      << "| " << piece.color << "\n"
         << setw(col_width) << "| Type:"       << "| " << piece.type << "\n";
    // Hu矩
    cout << "├─────────────────── Hu Moments ───────────────────┤\n";
    for (size_t i = 0; i < piece.huMoments.size(); ++i) {
        cout << "| hu" << (i+1) << ": " << scientific << piece.huMoments[i] 
             << ((i%2) ? "\n" : "\t");  // 每行显示两个矩
    }
    cout << "\n";
    // 几何特征
    cout << "├─────────────── Geometric Features ─────────────────┤\n"
         << setw(col_width) << "| Area:"       << "| " << fixed << piece.area << "\n"
         << setw(col_width) << "| Perimeter:"  << "| " << piece.perimeter << "\n"
         << setw(col_width) << "| AspectRatio:"<< "| " << piece.aspectRatio << "\n"; 
    // 区域密度
    cout << "├──────────────── Zone Density ─────────────────────┤\n| ";
    for (auto zd : piece.zoneDensity) {
        cout << setw(8) << zd << " ";
    }
    cout << "\n";  
    // SIFT关键点
    cout << "├──────────────── SIFT Keypoints (" << piece.siftKeypoints.size() << ") ────────────────┤\n";
    if (!piece.siftKeypoints.empty()) {
        // 打印前3个关键点作为示例
        for (size_t i = 0; i < min(3UL, piece.siftKeypoints.size()); ++i) {
            const auto& kp = piece.siftKeypoints[i];
            cout << "| Keypoint " << i+1 << ": "
                 << "Pos(" << setw(5) << kp.pt.x << ", " << setw(5) << kp.pt.y << ")"
                 << " Size:" << setw(5) << kp.size
                 << " Angle:" << setw(6) << kp.angle
                 << " Response:" << kp.response << "\n";
        }
        if (piece.siftKeypoints.size() > 3) {
            cout << "| ... (showing first 3 of " << piece.siftKeypoints.size() << ")\n";
        }
    } else {
        cout << "| No SIFT keypoints detected\n";
    }
    // SIFT描述子
    cout << "├─────────────── SIFT Descriptors (" 
         << piece.siftDescriptors.rows << "x" << piece.siftDescriptors.cols 
         << ") ───────────────┤\n";
    if (!piece.siftDescriptors.empty()) {
        // 打印第一个描述子的前5个维度作为示例
        cout << "| First descriptor sample: [";
        for (int j = 0; j < min(5, piece.siftDescriptors.cols); ++j) {
            cout << setw(8) << piece.siftDescriptors.at<float>(0, j);
        }
        cout << " ... ]\n";
    } else {
        cout << "| No SIFT descriptors\n";
    } 
    cout << "└───────────────────────────────────────────────────┘\n\n";
    // 恢复原始格式
    cout.flags(original_flags);
}

int main() {
    // 设置根目录路径（根据你的实际路径修改）
   // std::string rootDirectory = "/mnt/hgfs/aic_jeff/final_project/3.棋局获取显示/3.1棋子检测识别/棋子素材";
    // 开始处理所有棋子
 // processChessPieces(rootDirectory);
   
  // std::string testImagePath = "/mnt/hgfs/aic_jeff/final_project/3.棋局获取显示/3.1棋子检测识别/Project1/Project1/红车.jpg"; 
  // testChessRecognition(testImagePath);
	
	
	//ChessDatabase db("test.db");
	
	cv::Mat croppedImage;
	
	try {
        ImageProcessor processor("/mnt/hgfs/aic_jeff/final_project/3.棋局获取显示/3.1棋子检测识别/Project1/Project1/黑马.jpg");
        
        if (processor.processImage()) {
            cv::imshow("原始图像", processor.getResultImage());
            cv::imshow("裁剪结果", processor.getCroppedImage());
			croppedImage=processor.getCroppedImage();
            cv::waitKey(0);
        }
    } catch (const std::exception& e) {
        std::cerr << "错误: " << e.what() << std::endl;
        //return -1;
    }
	ChessDatabase::ChessPiece piece=extractChessPieceFeatures(
	croppedImage,
	1,
	2,
	"pawn"
	);
	printChessPiece(piece);
	
	//db.addChessPiece(piece);

   
    return 0;
}